import 'package:flutter/material.dart';
import '../utils/errors.dart';
import '../utils/error_handler.dart';
import '../theme/app_theme.dart';

class ErrorDisplay {
  // 显示带重试按钮的错误对话框
  static void showErrorWithRetry(BuildContext context, AppError? error, VoidCallback? onRetry) {
    // 如果没有错误对象，使用默认错误消息
    final errorMessage = error?.message ?? '发生未知错误';
    
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('错误'),
        content: Text(errorMessage),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('取消'),
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
              if (onRetry != null) {
                onRetry();
              } else if (error?.retryCallback != null) {
                error!.retryCallback!();
              }
            },
            child: Text('重试'),
          ),
        ],
      ),
    );
  }

  // 显示错误对话框
  static void showErrorDialog(BuildContext context, AppError error) {
    final String message = ErrorHandler().getUserFriendlyMessage(error);
    final String title = _getErrorTitle(error.type);
    
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title, style: TextStyle(color: AppTheme.red)),
          content: Text(message),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                // 如果是可恢复的错误，可以添加重试逻辑
                if (error.isRecoverable && error.retryCallback != null) {
                  error.retryCallback?.call();
                }
              },
              child: Text(
                error.isRecoverable && error.retryCallback != null ? '重试' : '确定',
                style: TextStyle(color: AppTheme.primaryColor),
              ),
            ),
            if (error.isRecoverable && error.retryCallback != null)
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text(
                  '取消',
                  style: TextStyle(color: AppTheme.textSecondary),
                ),
              ),
          ],
        );
      },
    );
  }
  
  // 显示错误Snackbar
  static void showErrorSnackbar(BuildContext context, AppError error) {
    final String message = ErrorHandler().getUserFriendlyMessage(error);
    
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: error.type == AppErrorType.validation
            ? AppTheme.red
            : AppTheme.red,
        behavior: SnackBarBehavior.floating,
        duration: const Duration(seconds: 3),
        action: error.isRecoverable && error.retryCallback != null
            ? SnackBarAction(
                label: '重试',
                textColor: Colors.white,
                onPressed: () {
                  error.retryCallback?.call();
                },
              )
            : null,
      ),
    );
  }
  
  // 显示加载对话框
  static void showLoadingDialog(BuildContext context, String message) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return PopScope(
      canPop: false,
      child: AlertDialog(
        content: Row(
          children: [
            CircularProgressIndicator(color: AppTheme.primaryColor),
            const SizedBox(width: 16),
            Expanded(child: Text(message)),
          ],
        ),
      ),
    );
      },
    );
  }
  
  // 显示成功提示
  static void showSuccessSnackbar(BuildContext context, String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: AppTheme.green,
        behavior: SnackBarBehavior.floating,
        duration: const Duration(seconds: 2),
      ),
    );
  }
  
  // 获取错误标题
  static String _getErrorTitle(AppErrorType type) {
    switch (type) {
      case AppErrorType.database:
        return '数据库错误';
      case AppErrorType.validation:
        return '输入验证错误';
      case AppErrorType.permission:
        return '权限不足';
      case AppErrorType.network:
        return '网络错误';
      case AppErrorType.storage:
        return '存储错误';
      case AppErrorType.business:
        return '业务错误';
      default:
        return '发生错误';
    }
  }
}

